#include "DBHelper.h"
#include "sampleSys_def.h"

DBhelper* DBhelper::s_head = NULL;

DBhelper::DBhelper() {
	pMySql = NULL;
	istrue = true;
	init();
}

DBhelper::~DBhelper() {
}

DBhelper* DBhelper::getIt() {
	if (s_head == NULL) {
		s_head = new(DBhelper);
	}
	return s_head;
}

bool DBhelper::init() {
	pMySql = mysql_init((MYSQL*)0);

	if (pMySql == NULL) {
		return false;
	}
	
	istrue = mysql_real_connect(pMySql,
		"172.81.208.192",
		"root",
		"#mzbc#xndx",
		"mysql",
		3306,
		NULL,
		0);
	return true;
}

void DBhelper::writeToDB(unsigned long long tick, int tag, int value, unsigned char* devId) {
	if (!mysql_select_db(pMySql, "mysql")) {
		char pQuery[BUFFER_SIZE] = { 0 };
		sprintf_s(pQuery, "INSERT INTO collect_info ( devID, time, tag, value )VALUES( '%s', %lld, %d, %d )", devId, tick, tag, value);
		//char* pQuery = "update testtable set name='123' where name='124'";

		int ret = mysql_real_query(pMySql, pQuery, strlen(pQuery));
		if (ret)
		{
			//error
			int i = 0;
			i++;
		}
		else
		{
			//right
			int i = 0;
			i++;
		}
	}
}


